package com.example.lancooznbk.view.activity;

import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceError;
import android.webkit.WebResourceRequest;
import android.webkit.WebResourceResponse;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;

import com.example.lancooznbk.R;
import com.example.lancooznbk.model.result.ReviewProgressEntity;
import com.example.lancooznbk.presenter.ReviewProgressPresenter;
import com.example.lancooznbk.view.base.BaseActivity;
import com.example.lancooznbk.view.base.ILoadDataView;
import com.example.lancooznbk.view.widget.EmptyMainLayout;

/**
 * 复习进度 目前在Android 4.4 还有部分显示不完全的bug TODO
 */
public class ReviewProcessActivity extends BaseActivity implements ILoadDataView<ReviewProgressEntity>{


    //private static final String URL = "http://172.16.41.34:8051/Index.aspx?SubjectID=S2-English&XH=hsab08011224&SysID=P10&IsMobile=1";
    private boolean isLoadError;
    Handler handler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message msg) {
            //mEmptyViewLayout.setVisibility(View.VISIBLE);
            switch (msg.what) {
                case 1:
                    showNetError();
                    progress.setVisibility(View.GONE);
                    return true;
                case 2:
                    hideLoading();
                    progress.setVisibility(View.GONE);
                    return true;
                case 3:
                    showLoading();
                    progress.setVisibility(View.VISIBLE);
                    progress.setProgress((Integer) msg.obj);
            }
            return false;

        }
    });
    private WebView webView;
    private ProgressBar progress;

    @Override
    public int attachLayoutRes() {
        return R.layout.activity_review_process;
    }

    @Override
    protected void initViews() {
        mEmptyViewLayout = (EmptyMainLayout) findViewById(R.id.layout_loading);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        //toolbar.setNavigationIcon(R.mipmap.ic_back);
        initToolBar(toolbar,true,R.string.review_progress);
        webView = (WebView) findViewById(R.id.web);
        progress = (ProgressBar) findViewById(R.id.progress);
        WebSettings settings = webView.getSettings();
        settings.setUseWideViewPort(true);//自适应
        settings.setLoadWithOverviewMode(true);//自适应
        settings.setJavaScriptEnabled(true);

        settings.setDomStorageEnabled(true);




        webView.setWebChromeClient(new WebChromeClient(){
            @Override
            public void onProgressChanged(WebView view, int newProgress) {


                //super.onProgressChanged(view, newProgress);
                if (newProgress == 100){
                    if (isLoadError){
                        handler.sendEmptyMessage(1);
                    }else {
                        handler.sendEmptyMessage(2);
                    }
                }else {
                    Message msg = new Message();
                    msg.what = 3;
                    msg.obj = newProgress;
                    handler.sendMessage(msg);

                }
            }

            @Override
            public void onReceivedTitle(WebView view, String title) {
                super.onReceivedTitle(view, title);
                if (!TextUtils.isEmpty(title) &&
                        (title.trim().toLowerCase().contains("error")|| title.contains("找不到网页")))
                {
                    isLoadError = true;
                    handler.sendEmptyMessage(1);
                }else {
                    isLoadError = false;
                }
            }
        });


    }

    @Override
    protected void updateViews(boolean isRefresh) {
        ReviewProgressPresenter presenter = new ReviewProgressPresenter(this);
        presenter.getData(true);
       /* if (mEmptyViewLayout.getEmptyStatus() == EmptyMainLayout.STATUS_NO_NET){
            webView.reload();
        }*/

    }


    @Override
    public void loadData(ReviewProgressEntity data) {
        webView.loadUrl(data.getUrl());
        webView.setWebViewClient(new WebViewClient(){
                                     @Override
                                     public boolean shouldOverrideUrlLoading(WebView view, String url) {
                                         view.loadUrl(url);
                                         return true;
                                     }

                                 });
    }
}
